<%@ page import="cn.myschool.entity.Student" %>
<%@ page import="cn.myschool.service.StudentService" %>
<%@ page import="java.sql.Date" %>
<%@ page import="java.util.UUID" %>
<%@ page import="cn.myschool.util.StrUtil" %>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%@ page import="org.apache.commons.fileupload.FileItem" %>
<%@ page import="java.util.List" %>
<%@ page import="java.io.File" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>

<body>
<%@include file="checkLogin.jsp" %>

<%
    DiskFileItemFactory d = new DiskFileItemFactory();
    ServletFileUpload sp = new ServletFileUpload(d);
    sp.setHeaderEncoding("utf-8");
    List<FileItem> fileItems = sp.parseRequest(request);
    String sname = fileItems.get(0).getString("utf-8");
    String sex = fileItems.get(1).getString("utf-8");
    String telephone = fileItems.get(2).getString();
    String qq = fileItems.get(3).getString();
    String wechat = fileItems.get(4).getString("utf-8");
    String fromcity = fileItems.get(5).getString("utf-8");
    String address = fileItems.get(6).getString("utf-8");
    String pass = fileItems.get(7).getString("utf-8");
    String idcardpic = fileItems.get(8).getString("utf-8");
    String enterdate = fileItems.get(9).getString();
    String birthday = fileItems.get(10).getString();
    String gradeid = fileItems.get(11).getString();

    FileItem f = fileItems.get(12);
    //out.print("这是一个文件域,名字：" + f.getFieldName());
    if (f.getSize() >= 1) {
        String fileName = f.getName();
        String houzhui = fileName.substring(fileName.lastIndexOf('.'));
        String newname = UUID.randomUUID() + houzhui;
        File newPic = new File(application.getRealPath("images"));
        if (!newPic.exists())
            newPic.mkdir();
        f.write(new File(newPic, newname));
    }

    Date enter = StrUtil.toSqlDate(StrUtil.toDate(enterdate));
    Date birth = StrUtil.toSqlDate(StrUtil.toDate(birthday));
    Student student = new Student(sname, sex, telephone, qq, wechat, Integer.parseInt(gradeid), enter, fromcity,
            birth, address, pass, idcardpic);
    int r = StudentService.ME.add(student);

    if (r > 0) {
        response.sendRedirect("student.jsp");
    } else {
        request.setAttribute("msg", "增加失败，可能重复了！");
        request.getRequestDispatcher("studentAdd.jsp").forward(request, response);
    }
%>
</body>
</html>
